﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.Diagnostics;

namespace ChamCong
{
    public partial class genData : System.Web.UI.Page
    {
        CongBLL cBLL = new CongBLL();
        NhanVienBLL nvBLL = new NhanVienBLL();
        int Thang, Nam, count;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindDdlYear();
            }
            Thang = Convert.ToInt32(ddlMonth.SelectedValue);
            Nam = Convert.ToInt32(ddlYear.SelectedValue);

        }
        public DateTime dtmGen(int _Day, int _Month, int _Year)
        { 
            DateTime dtm = Convert.ToDateTime(_Day + "-" + _Month + "-" + _Year);
            return dtm;
        }
        public void bindDdlYear()
        {
            for (int i = DateTime.Now.Year; i >= 2010; i--)
            {
                ListItem obj = new ListItem();
                obj.Text = i.ToString();
                obj.Value = i.ToString();
                ddlYear.Items.Add(obj);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem item = new ListItem();
                item.Text = "Tháng " + i;
                item.Value = i.ToString();
                ddlMonth.Items.Add(item);
            }
        }
       
        protected void btnGenCong_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                Thang = 01;
                
                List<NHANVIEN> lstTatCaNV = nvBLL.LayTatCa();
                //DateTime dtm = Convert.ToDateTime("2012-" + Thang + "-01");
                for (int j = 1; j < DateTime.DaysInMonth(Nam, Thang)+1; j++)
                {
                    for (int i = 0; i < lstTatCaNV.Count; i++){
                        CONG c = new CONG();
                        c.Ngay = dtmGen(Thang, j, Nam);
                        c.MaNV = lstTatCaNV[i].MaNV;
                        c.LoaiChamCong = 1;
                        c.TrangThaiChot = 0;
                        if(cBLL.KiemTraTonTai(c.MaNV, c.Ngay) == 0 && c.Ngay.DayOfWeek != DayOfWeek.Sunday)
                        {
                            Random r = new Random();
                            Random rr = new Random();
                            if (r.Next(0, 2) == 0)
                            {
                                c.GioDen = dtmGen(Thang, j, Nam).AddHours(7).AddMinutes(r.Next(50, 59));
                                c.GioVe = dtmGen(Thang, j, Nam).AddHours(17).AddMinutes(rr.Next(j, 59));
                            }
                            else
                            {
                                c.GioDen = dtmGen(Thang, j, Nam).AddHours(8).AddMinutes(r.Next(Thang, 32));
                                c.GioVe = dtmGen(Thang, j, Nam).AddHours(17).AddMinutes(rr.Next(Thang, 59));
                            }
                            cBLL.ThemCong(c);
                            count++;
                            lblTest.Text = "Đã gen " + count + " records";                            
                        }                        
                    }

                }
                
            }
            catch (Exception ex)
            {

                lblTest.Text = ex.Message;
            }
            stopwatch.Stop();
            lblTest.Text += ". Thời gian thực thi: " + stopwatch.Elapsed;
        }
    }
}